package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.BompMchntLevelInfoMapper;
import cc.rengu.igas.bomp.common.dao.InspectTaskDetailMapper;
import cc.rengu.igas.bomp.common.dao.InspectTaskInfoMapper;
import cc.rengu.igas.bomp.common.dao.OrganizationMapper;
import cc.rengu.igas.bomp.common.dao.impl.BompMchntLevelInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskDetailMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.InspectTaskInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.OrganizationMapperImpl;
import cc.rengu.igas.bomp.common.entity.BompMchntLevelInfo;
import cc.rengu.igas.bomp.common.entity.InspectTaskDetail;
import cc.rengu.igas.bomp.common.entity.InspectTaskInfo;
import cc.rengu.igas.bomp.common.entity.Organization;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.InspectTaskDetailBean;
import cc.rengu.igas.bomp.facade.request.InspectTaskDetailListQueryRequest;
import cc.rengu.igas.bomp.facade.response.InspectTaskDetailListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/03/31
 */
public class InspectTaskDetailListQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        InspectTaskDetailListQueryRequest inspectTaskDetailListQueryRequest = new InspectTaskDetailListQueryRequest();
        ConvertUtil.convertOutput(inspectTaskDetailListQueryRequest);
        return inspectTaskDetailListQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {

        InspectTaskDetailListQueryRequest inspectTaskDetailListQueryRequest = (InspectTaskDetailListQueryRequest) request;
        InspectTaskDetailListQueryResponse inspectTaskDetailListQueryResponse = new InspectTaskDetailListQueryResponse();
        /* 获取法人行标识 */
        String instId = inspectTaskDetailListQueryRequest.getHeader().getInstId();
        /*获取巡检任务号*/
        String inspectTaskNo = inspectTaskDetailListQueryRequest.getInspectTaskNo();
        /*获取用户信息*/
        String userId = inspectTaskDetailListQueryRequest.getUserId();
        /*非空必填校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(inspectTaskNo)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*商户号*/
        String mchntNo = inspectTaskDetailListQueryRequest.getMchntNo();
        /*商户名称*/
        String mchntName = inspectTaskDetailListQueryRequest.getMchntName();
        /*签约机构号*/
        String signInstId = inspectTaskDetailListQueryRequest.getSignInstId();
        /*巡检机构号*/
        String inspectInstId = inspectTaskDetailListQueryRequest.getInspectInstId();
        /*巡检状态*/
        String inspectStatus = inspectTaskDetailListQueryRequest.getInspectStatus();
        /*商户类型*/
        String mchntType = inspectTaskDetailListQueryRequest.getMchntType();
        /*商户等级*/
        String mchntLevel = inspectTaskDetailListQueryRequest.getMchntLevel();
        /*拓展经理工号*/
        String managerId = inspectTaskDetailListQueryRequest.getManagerId();
        /*获取分页信息*/
        int pageIndex = inspectTaskDetailListQueryRequest.getPageIndex();
        int pageSize = inspectTaskDetailListQueryRequest.getPageSize();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(pageIndex);
        pageInfo.setPageSize(pageSize);
        /*任务描述初始化*/
        String inspectTaskDesc = "";
        /*根据巡检任务编号巡检任务名称查询*/
        InspectTaskInfoMapper inspectTaskInfoMapper = new InspectTaskInfoMapperImpl();
        InspectTaskInfo inspectTaskInfo = inspectTaskInfoMapper.selectInspectTaskInfoByPrimaryKey(instId, inspectTaskNo);
        if (null != inspectTaskInfo) {
            inspectTaskDesc = inspectTaskInfo.getInspectTaskDesc();
        }
        /*根据巡检任务编号查询任务详情列表*/
        InspectTaskDetail inspectTaskDetailFilter = new InspectTaskDetail();
        inspectTaskDetailFilter.setInstId(instId);
        inspectTaskDetailFilter.setInspectTaskNo(inspectTaskNo);
        inspectTaskDetailFilter.setMchntNo(StringUtil.isEmptyOrNull(mchntNo) ? mchntNo : "%" + mchntNo + "%");
        inspectTaskDetailFilter.setMchntName(StringUtil.isEmptyOrNull(mchntName) ? mchntName : "%" + mchntName + "%");
        inspectTaskDetailFilter.setSignInstId(signInstId);
        inspectTaskDetailFilter.setInspectInstId(inspectInstId);
        inspectTaskDetailFilter.setInspectStatus(inspectStatus);
        inspectTaskDetailFilter.setMchntType(mchntType);
        inspectTaskDetailFilter.setMchntLevel(mchntLevel);
        inspectTaskDetailFilter.setManagerId(managerId);
        InspectTaskDetailMapper inspectTaskDetailMapper = new InspectTaskDetailMapperImpl();
        PageResult pageResult = inspectTaskDetailMapper.selectInspectTaskDetailByQueryFilter(inspectTaskDetailFilter, pageInfo);
        if (null == pageResult) {
            throw new BizException(RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespCode(), RespCodeEnum.QUERY_BASEDATA_IS_NULL.getRespDesc());
        }
        pageInfo = pageResult.getPageInfo();
        List<InspectTaskDetail> inspectTaskDetailQueryList = pageResult.getResult().stream().map(item -> (InspectTaskDetail) item).collect(Collectors.toList());
        List<InspectTaskDetailBean> inspectTaskDetailList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inspectTaskDetailQueryList)) {
            /*查询组织机构*/
            OrganizationMapper organizationMapper = new OrganizationMapperImpl();
            /*查询商户等级*/
            BompMchntLevelInfoMapper bompMchntLevelInfoMapper = new BompMchntLevelInfoMapperImpl();
            for (InspectTaskDetail inspectTaskDetail : inspectTaskDetailQueryList) {
                InspectTaskDetailBean inspectTaskDetailBean = new InspectTaskDetailBean();
                BeanUtil.beanCopy(inspectTaskDetail, inspectTaskDetailBean);
                inspectTaskDetailBean.setInspectTaskDesc(inspectTaskDesc);
                /*根据签约机构编码查询签约机构编码*/
                if (null != inspectTaskDetail.getSignInstId() && !inspectTaskDetail.getSignInstId().isEmpty()) {
                    Organization organization = organizationMapper.selectOrganizationByPrimaryKey(instId, inspectTaskDetail.getSignInstId());
                    if (null != organization) {
                        inspectTaskDetailBean.setSignInstName(organization.getOrgName());
                    }
                }
                /*根据巡检机构编码查询巡检机构编码*/
                if (null != inspectTaskDetail.getInspectInstId() && !inspectTaskDetail.getInspectInstId().isEmpty()) {
                    Organization organization = organizationMapper.selectOrganizationByPrimaryKey(instId, inspectTaskDetail.getInspectInstId());
                    if (null != organization) {
                        inspectTaskDetailBean.setInspectInstName(organization.getOrgName());
                    }
                }
                /*根据商户等级编码查询商户等级名称信息*/
                if (null != inspectTaskDetail.getMchntLevel() && !inspectTaskDetail.getMchntLevel().isEmpty()) {
                    BompMchntLevelInfo bompMchntLevelInfo = bompMchntLevelInfoMapper.selectBompMchntLevelInfoByMchntLevelCode(instId, inspectTaskDetail.getMchntLevel());
                    if (null != bompMchntLevelInfo) {
                        inspectTaskDetailBean.setMchntLevelName(bompMchntLevelInfo.getMchntLevelName());
                    }
                }
                inspectTaskDetailList.add(inspectTaskDetailBean);
            }
        }
        /*  根据分页查询条件截取相关数据记录 */
        inspectTaskDetailListQueryResponse.setPageIndex(pageInfo.getPageIndex());
        inspectTaskDetailListQueryResponse.setPageSize(pageInfo.getPageSize());
        inspectTaskDetailListQueryResponse.setTotalRows(pageInfo.getTotalRows());
        inspectTaskDetailListQueryResponse.setTotalPage(pageInfo.getTotalPage());
        inspectTaskDetailListQueryResponse.setInspectTaskDetailList(inspectTaskDetailList);
        /* 按照接口转换基础产品信息并响应给请求方 */
        BizResponse<InspectTaskDetailListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(inspectTaskDetailListQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        InspectTaskDetailListQueryResponse inspectTaskDetailListQueryResponse = (InspectTaskDetailListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(inspectTaskDetailListQueryResponse);
    }
}
